//webpart Properties
var _Wlistname;
var _Wauto;
var _Wvisible;
var _Wverticle;
var _Wrtl;
var _arrows;
var _circular;
var _margin;
var _width;
var _height;
var _textLinks;

function GenerateListItem(associativeArray) {
    var linkURL = associativeArray["url"];
    var url = associativeArray["path"];
    var linkDiscription = associativeArray["dis"];
    var anchor = $(document.createElement("a"));
    var container = $(document.createElement("div"));
    var image = $(document.createElement("img"));
    var text = $(document.createElement("div"));
    var listItem = $(document.createElement("li"));
    anchor.attr("href", linkURL);
    container.attr("class", "pn2");
    container.css("margin", _margin + "px");
    image.attr("src", '/' + url);
    image.attr("width", _width);
    image.attr("height", _height);
    //text.css("text-align", "center");
    container.append(image);
    if (_textLinks) {
        text.attr("class", "CarouselText");
        text.text(linkDiscription);
        container.append(text);
    }
    //container.append(text);
    anchor.append(container);
    listItem.append(anchor);
    return listItem
}

function initilizeProperties(carouselID) {
    var carouselPropDiv = $('#' + carouselID + '_SPCarouselProperties');
    _Wlistname = carouselPropDiv.find("#ListName").text();
    var visable = carouselPropDiv.find("#Visible").text();
    _Wvisible = parseInt(visable);
    _Wauto = parseInt(carouselPropDiv.find("#Auto").text());
    var temp = carouselPropDiv.find("#TextLinks").text()
    if (temp == "True") {
        _textLinks = true;
    } else {
        _textLinks = false;
    }
    if (carouselPropDiv.find("#Verticle").text() == "True") {
        _Wverticle = true;
    } else {
        _Wverticle = false;
    }
    if (carouselPropDiv.find("#Arrows").text() == "True") {
        _arrows = "<div></div>";
    } else {
        _arrows = null;
    }
    _circular = carouselPropDiv.find("#Wrap").text();
    _margin = carouselPropDiv.find("#margin").text();
    _Wrtl = false;
    _width = carouselPropDiv.find("#Width").text();
    _height = carouselPropDiv.find("#Height").text();
}

function GetData() {
    var Data = new Array();
    var i = 0;
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: _Wlistname,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='SlidesContent' /><FieldRef Name='URL' /></ViewFields>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                Data[i] = $(this);
                i++;
            });
        }
    });
    return Data;
}

function CreateAssociativeArray(dataObject) {
    var returnArray = new Array();
    var path = dataObject.attr("ows_FileRef");
    var urlRaw = dataObject.attr("ows_URL");
    var url = urlRaw.split(', ')[0]
    var dis = urlRaw.split(', ')[1]
    returnArray["path"] = path.split(';#')[1]; ;
    returnArray["url"] = url;
    returnArray["dis"] = dis;
    return returnArray;
}

function IntilizeSPCarousel(carouselID) {
    var carouselID = carouselID;
    initilizeProperties(carouselID)
    var data = GetData();
    var index = 0;
    var carouselID = "#" + carouselID + '_SPCarousel';
    var listItems = new Array();
    var carousel = $(carouselID);
    for (var i = 0; i < data.length; i++) {
        var associativeArray = CreateAssociativeArray(data[i]);
        var generatedListItem = GenerateListItem(associativeArray);
        carousel.append(generatedListItem);
    }
    //mycarousel INIT
    $(carouselID).jcarousel({
        visible: _Wvisible,
        scroll: 1,
        wrap: _circular,
        auto: _Wauto,
        vertical: _Wverticle,
        buttonNextHTML: _arrows,
        buttonPrevHTML: _arrows
    });

    $(carouselID).css("height", _height + 50 + "px");
}
$(document).ready(function () {
    $("body").find("#SPCarouselRegister").each(function () {
        carouselID = $(this).text();
        IntilizeSPCarousel(carouselID);
    });
});